package cn.kailangtu.modbus;

import android.util.Log;

import org.json.JSONObject;

import cn.kailangtu.commontools.APPSPUtils;
import cn.kailangtu.commontools.CommonUtils;

public class ModbusCommand {
    protected byte address;
    protected byte function;
    protected byte[] store;
    protected byte[] data;
    protected byte[] crc;


    /***
     * 01,非法功能；02，非法数据地址，03，非法数据值，-1校验不通过
     */
    int errCode = 0;

    public byte getAddress() {
        return address;
    }

    public void setAddress(byte address) {
        this.address = address;
    }

    public byte getFunction() {
        return function;
    }

    public void setFunction(byte function) {
        this.function = function;
    }

    public byte[] getStore() {
        return store;
    }

    public void setStore(byte[] store) {
        this.store = store;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public byte[] getCrc() {
        return crc;
    }

    public void setCrc(byte[] crc) {
        this.crc = crc;
    }




    public byte[] errData(int errCode) {
        byte[] result = new byte[5];
        result[0] = address;
        result[1] = (byte) (function + 0x80);
        result[2] = (byte) errCode;

        byte[] check = CRC16.getCheckCMDBytes(CRC16.calcCrc16(result, 0, result.length - 2));
        result[3] = check[0];
        result[4] = check[1];

        return result;
    }

    public byte[] receive(byte[] data)
    {
        byte[] result = new byte[data.length+4];
        result[0] = address;
        result[1] = function;
        for(int i = 0;i<data.length ;i++)
        {
            result[i+2] = data[i];
        }


        byte[] check = CRC16.getCheckCMDBytes(CRC16.calcCrc16(result, 0, result.length - 2));
        result[result.length-2] = check[0];
        result[result.length-1] = check[1];

        return result;
    }





    public static ModbusCommand buildCommand(String command)
    {
        return buildCommand(CommonUtils.hexStringToByteArray(command));
    }

    public static ModbusCommand buildCommand(byte[] command)
    {

        byte[] checkBytes = CRC16.getCheckCMDBytes( CRC16.calcCrc16(command,0,command.length-2));

        if(checkBytes.length == 2 && checkBytes[0] == command[command.length-2] && command[command.length -1] == checkBytes[1])
        {
            byte address = APPSPUtils.getInstance().getModbusSlaveAddress();
            if(command[0] == address)
            {
                ModbusCommand modbusCommand = new ModbusCommand();
                modbusCommand.setAddress(command[0]);
                modbusCommand.setCrc( new byte[]{command[command.length-2],command[command.length -1]});
                modbusCommand.setFunction(command[1]);
                modbusCommand.setStore(new byte[]{command[2],command[3]});



                byte[] currData = new byte[command.length-6];
                for(int i = 4;i< command.length -2;i++)
                {
                    currData[i-4] = command[i];
                }
                modbusCommand.setData(currData);

                Log.e("MODBUS",""+modbusCommand.getAddress());
                Log.e("MODBUS",""+modbusCommand.getFunction());
                Log.e("MODBUS",""+CommonUtils.ByteArrayToHexString(modbusCommand.getStore()));
                Log.e("MODBUS",""+CommonUtils.ByteArrayToHexString(modbusCommand.getData()));
                Log.e("MODBUS",""+CommonUtils.ByteArrayToHexString( modbusCommand.getCrc()));


                return modbusCommand;
            }


        }


        return null;
    }
}
